<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!-- 搜索 -->
      <el-input
        v-model="query.databaseName"
        clearable
        size="small"
        placeholder="请输入库名"
        style="width:200px"
        class="filter-item"
      />
      <el-input
        v-model="query.tableName"
        clearable
        size="small"
        placeholder="请输入表名"
        style="width:200px"
        class="filter-item"
      />
      <el-input
        v-model="query.fieldName"
        clearable
        size="small"
        placeholder="请输入表名"
        style="width:200px"
        class="filter-item"
      />
      <rrOperation :crud="crud"/>
    </div>
    <div style="margin-bottom: 10px">
      <el-button class="filter-item" icon="el-icon-plus" type="primary" @click="crud.toAdd()">新增</el-button>
      <el-button v-if="crud.optShow.del" slot="reference" v-permission="permission.del" class="filter-item"
                 type="danger" icon="el-icon-delete" :loading="crud.delAllLoading"
                 :disabled="crud.selections.length === 0"
                 @click="toDelete(crud.selections)">移除
      </el-button>
    </div>
    <!--表格渲染-->
    <el-table
      ref="table"
      v-loading="crud.loading"
      :data="crud.data"
      border
      max-height="300px"
      @select="crud.selectChange"
      @select-all="crud.selectAllChange"
      @selection-change="crud.selectionChangeHandler"
    >
      <el-table-column type="selection" width="40"/>
      <el-table-column label="库名" prop="databaseName" width="150px"/>
      <el-table-column label="表名" prop="tableName" width="200px"/>
      <el-table-column label="字段名" prop="fieldName"/>
      <el-table-column label="创建人" prop="createBy" width="100px"/>
      <el-table-column label="创建时间" prop="createTime" width="135px"/>
    </el-table>
    <div style=" float: right;">
      <!--分页组件-->
      <pagination/>
    </div>

    <el-dialog
      :close-on-click-modal="false"
      :before-close="crud.cancelCU"
      :visible.sync="crud.status.add > 0"
      title="新增黑名单"
      width="500px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="simpleRules" :inline="true" label-width="100px">
        <el-row>
          <el-form-item prop="databaseName" label="库名称">
            <el-select
              filterable
              v-model="form.databaseName"
              @change="databaseNameChange"
              style="width:260px"
            >
              <el-option v-for="item in databaseList" :key="item.name" :value="item.name"/>
            </el-select>
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item prop="tableName" label="表名称">
            <el-select
              filterable
              v-model="form.tableName"
              @change="tableNameChange"
              style="width:260px"
            >
              <el-option v-for="item in tableList" :key="item.tblName" :label="item.tblName" :value="item.tblName"/>
            </el-select>
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item prop="fieldNameList" label="字段名称">
            <el-select
              filterable
              multiple
              v-model="form.fieldNameList"
              style="width:260px"
            >
              <el-option v-for="item in fieldName" :key="item.fieldName" :label="item.fieldName" :value="item.fieldName"/>
            </el-select>
          </el-form-item>
        </el-row>
        <div class="dialog-footer" style="width:100%;text-align: center">
          <el-button @click="crud.cancelCU()">关 闭</el-button>
          <el-button type="primary" @click="crud.submitCU">保 存</el-button>
        </div>
      </el-form>
    </el-dialog>

  </div>
</template>

<script>
import desensitizationFieldFilterApi from '@/api/dashboard/desensitizationFieldFilter'
import CRUD, {presenter, header, form, crud} from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import pagination from '@crud/Pagination'
import sourceRuleTableViewApi from '@/api/dataTransfer/common/sourceRuleTableView'
import cdhMetadataApi from '@/api/dataTransfer/common/cdhMetadata'

// crud交由presenter持有
const defaultCrud = CRUD({
  title: '脱敏黑名单',
  url: '/api/desensitize/field/filter/page',
  crudMethod: {...desensitizationFieldFilterApi}
})
const defaultForm = {
  id: null,
  databaseName: null,
  tableName: null,
  fieldName: null,
  fieldNameList: []
}
export default {
  name: 'DataDesensitizeFieldFilter',
  components: {rrOperation, pagination},
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
  data() {
    return {
      simpleRules: {
        databaseName: [
          {required: true, message: '请选择库名', trigger: 'change'}
        ],
        tableName: [
          {required: true, message: '请选择表名', trigger: 'change'}
        ],
        fieldNameList: [
          {required: true, message: '请选择字段', trigger: 'change'}
        ]
      },
      permission: {
        add: ['admin', 'desensitizationFieldFilter:add'],
        del: ['admin', 'desensitizationFieldFilter:del']
      },
      tableList: [],
      databaseList: [],
      fieldName: []
    }
  },
  created() {
    this.loadDbList()
  },
  methods: {
    databasesList() {
      this.databaseList = []
      sourceRuleTableViewApi.databases().then(res => {
        this.databaseList = res
      })
    },
    databaseNameChange(databaseName) {
      this.tableList = []
      this.form.tableNameList = []
      cdhMetadataApi.tables(databaseName).then(res => {
        this.tableList = res
      })
    },
    tableNameChange(tableName) {
      this.fieldName = []
      const databaseName = this.form.databaseName
      cdhMetadataApi.columns(databaseName, tableName).then(res => {
        this.fieldName = res
      })
    },
    toDelete(datas) {
      this.$confirm(`确认删除选中的${datas.length}条数据?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.crud.delAllLoading = true
        this.crud.doDelete(datas)
      }).catch(() => {
      })
    },
    loadDbList() {
      this.databaseList = []
      this.sourceList = []
      this.form.sourceName = null
      this.form.databaseName = null
      this.form.tableNameList = []
      cdhMetadataApi.databases().then(res => {
        this.databaseList = res
      })
    }
  }
}
</script>
<style scoped>

</style>
